/*-------------------------------------------------------------------------------
* file:     app_spi.h
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __APP_SPI_H__
#define __APP_SPI_H__

/* Includes ------------------------------------------------------------------*/
#include "common.h"

#define SPI0_PIN_NSS            8
#define SPI0_PIN_MISO           10
#define SPI0_PIN_MOSI           6
#define SPI0_PIN_SCK            8

#define SPI0_PIN_AF_NSS         7   // AF4
#define SPI0_PIN_AF_MISO        7   // AF4
#define SPI0_PIN_AF_MOSI        8   // AF5
#define SPI0_PIN_AF_SCK         8   // AF5

#define FLASH_PAGE_SIZE                     (256)
#define FLASH_SECTOR_SIZE                   (4096)
#define FLASH_SECTOR_SIZE_1                   (8192)
#define FLASH_BLOCK32_SIZE                  (1024*32)
#define FLASH_BLOCK64_SIZE                  (1024*64)
#define CMD_FLASH_READ_STATUS0              0x05
#define CMD_FLASH_READ_STATUS1              0x35
#define CMD_FLASH_WRITE_STATUS              0x01
#define CMD_FLASH_WRITE_STATUS1             0x31
#define CMD_FLASH_WRITE_EN                  0x06
#define CMD_FLASH_WRITE_DISABLE             0x04
#define CMD_FLASH_SECTOR_ERASE              0x20
#define CMD_FLASH_BLOCK32_ERASE             0x52
#define CMD_FLASH_BLOCK64_ERASE             0xD8
#define CMD_FLASH_PAGE_PROGRAM              0x02
#define CMD_FLASH_READ                      0x03
#define CMD_FLASH_READ_UID    				      0x4B
#define CMD_FLASH_READ_MANUFAC              0x90
#define CMD_FLASH_UNLOCK_STATUS             0x01

#define  FLASH_NEED_LOCK_SIZE               192

typedef enum
{
    AREA0_00000_3FFFF,//LOWER 256K
    AREA1_00000_3FFFF,//LOWER 256K
    AREA_00000_1FFFF,//LOWER 128K
    AREA_00000_2FFFF,//LOWER 192K
    AREA_00000_37FFF,//LOWER 224K
    AREA_00000_3BFFF,//LOWER 240K
    AREA_00000_3DFFF,//LOWER 248K
    UNLOCK,        
}FLASH_LOCK_AREA_E;

void app_spi_master_init(void);
void app_spi_master_write(uint8_t *p_buf,uint32_t len);
uint32_t app_spi_master_write_read(uint8_t *p_wbuf,uint32_t wlen,uint8_t *p_rbuf,uint32_t max_rlen);
#if (APP_DEBUG_SPI_FLASH_ENABLE == 1)
  void app_spi_master_test(void);
#endif
void Nop_delay(int num);
void app_spi_flash_write_en(void);
uint16_t app_spi_flash_page_program(uint32_t offset,uint8_t *pdata,uint16_t length);
uint16_t app_spi_flash_page_read(uint32_t offset,uint8_t *pdata,uint16_t length);
int app_spi_flash_sector_or_block_erase(uint32_t addr,uint32_t erase_size);
uint32_t app_spi_master_read_flash_unique_id(uint8_t * pbuf);
uint32_t app_spi_master_read_flash_manufacture_id(void);

int app_spi_flash_busy(void);
uint16_t app_spi_flash_page_write(uint32_t offset,uint8_t *pdata,uint16_t length);

uint32_t app_spi_flash_locked_size(void);
uint32_t drv_flash_lock_config(FLASH_LOCK_AREA_E area);
void boot_flash_lock_flash(FLASH_LOCK_AREA_E area);
void boot_flash_unlock_flash(FLASH_LOCK_AREA_E area);
#endif